package
{
	import com.PQLoad.PQLoader;
	import com.tools.EventHelper;
	
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.net.URLRequest;
	import flash.utils.unescapeMultiByte;
	
	public class Editor extends Sprite
	{
		/**操作按钮**/
		private var menuBtn:menu;
		/**删除按钮**/
		private var btnDelete:recycle;
		/**当前处理的对象**/
		private var curObj:DisplayObject;
		/**显示容器**/
		private var uiPanel:Sprite;
		/**对象属性面板**/
		private var attrPanel:objAttr;
		/**菜单事件列表**/
		private var menuEvent:Array;
		/**菜单相关处理函数**/
		private var menuFunc:Array;
		/**属性面板相关处理函数**/
		private var attrFunc:Array;
		/**属性面板事件列表**/
		private var attrEvent:Array;
		public function Editor()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		private function addToStage(e:Event):void{
			initBaseView();
			stage.addEventListener(Event.RESIZE,resize);
		}
		/**
		 *创建UI 
		 * @param configList
		 * 
		 */		
		public function createView(configList:Array):void{
			trace("ok");
			for each(var obj:Object in configList){
				var p:Sprite=new Sprite();
				var img:DisplayObject;
				if(obj.res.substr(obj.res.length-4).toLocaleLowerCase()==".png")img=PQLoader.getPng(obj.res);
				else img=PQLoader.getImg(obj.res);
				p.addChild(img);
				p.mouseChildren=false;
				p.buttonMode=true;
				p.name=obj.res;
				p.x=obj.x;
				p.y=obj.y;
				p.addEventListener(MouseEvent.MOUSE_DOWN,drag);
				p.addEventListener(MouseEvent.MOUSE_UP,sdrag);
				p.addEventListener(MouseEvent.CLICK,resClick);
				//p.addEventListener(MouseEvent.MOUSE_WHEEL,setTop);
				uiPanel.addChild(p);
			}
		}
		/****/
		private function resize(e:Event):void{
			if(!uiPanel)return;
			uiPanel.x=0;
			uiPanel.y=0;
			menuBtn.x=stage.stageWidth-90;
			btnDelete.x=stage.stageWidth-btnDelete.width;
			btnDelete.y=stage.stageHeight-btnDelete.height;
		}
		/**初始化基础视图**/
		private function initBaseView():void{
			uiPanel=new Sprite();
			addChild(uiPanel);
			uiPanel.graphics.beginFill(0xffffff,0);
			uiPanel.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			attrPanel=new objAttr();
			attrPanel.visible=false;
			attrPanel.btn_bottom.buttonMode=true;
			attrPanel.btn_edit.buttonMode=true;
			attrPanel.btn_hideOther.buttonMode=true;
			attrPanel.btn_lock.buttonMode=true;
			attrPanel.btn_remove.buttonMode=true;
			attrPanel.btn_top.buttonMode=true;
			attrPanel.btn_unlock.buttonMode=true;
			addChild(attrPanel);
			btnDelete=new recycle();
			addChild(btnDelete);
			btnDelete.buttonMode=true;
			btnDelete.x=stage.stageWidth-btnDelete.width;
			btnDelete.y=stage.stageHeight-btnDelete.height;
			menuBtn=new menu();
			addChild(menuBtn);
			menuBtn.x=stage.stageWidth-90;
			
			menuEvent=["btn_submit","btn_showAll","btn_fullscreen","btn_top","btn_clear"];
			menuFunc=[traceConfig,showAll,fullScreen,setTop,clear];
			attrEvent=["btn_edit","btn_lock","btn_unlock","btn_top","btn_bottom","btn_hideOther","btn_remove"];
			attrFunc=[updateAttr,objLock,objUnlock,objTop,objBottom,hideOther,objRemove];
			
			btnDelete.addEventListener(MouseEvent.MOUSE_UP,delObj);
			
			stage.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, setDrag);
			stage.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, load);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL,setScale);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyChange);
			
			menuBtn.addEventListener(MouseEvent.CLICK,menuClick);
			attrPanel.addEventListener(MouseEvent.CLICK,attrPanelClick);
			attrPanel.bg.addEventListener(MouseEvent.MOUSE_DOWN,dragAttrPanel);
			attrPanel.bg.addEventListener(MouseEvent.MOUSE_UP,unDragAttrPanel);
		}
		private function dragAttrPanel(e:MouseEvent):void{
			attrPanel.startDrag();
		}
		private function unDragAttrPanel(e:MouseEvent):void{
			attrPanel.stopDrag();
		}
		private function menuClick(e:MouseEvent):void{
			EventHelper.send(e.target.name,e.target,menuEvent,menuFunc);
		}
		private function attrPanelClick(e:MouseEvent):void{
			trace(e.target.parent.name);
			EventHelper.send(e.target.parent.name,e.target.parent,attrEvent,attrFunc);
		}
		private function updateAttr(type:String,target:Object):void{
			var obj:*=uiPanel.getChildByName(attrPanel.txt_name.text);
			if(obj){
				if(attrPanel.txt_x.text.length>0)obj.x=parseInt(attrPanel.txt_x.text);
				if(attrPanel.txt_y.text.length>0)obj.y=parseInt(attrPanel.txt_y.text);
				if(attrPanel.txt_scalex.text.length>0)obj.scaleX=parseFloat(attrPanel.txt_scalex.text);
				if(attrPanel.txt_scaley.text.length>0)obj.scaleY=parseFloat(attrPanel.txt_scaley.text);
				if(attrPanel.txt_alpha.text.length>0)obj.alpha=parseFloat(attrPanel.txt_alpha.text);
				if(attrPanel.txt_visible.text=="false")obj.visible=false;
				else obj.visible=true;
			}else	trace("资源不存在");
		}
		private function objLock(type:String,target:Object):void{
			var obj:Object=uiPanel.getChildByName(attrPanel.txt_name.text);
			obj.mouseEnabled=false;
			obj.mouseChildren=false;
		}
		private function objUnlock(type:String,target:Object):void{
			var obj:Object=uiPanel.getChildByName(attrPanel.txt_name.text);
			obj.mouseEnabled=true;
			obj.mouseChildren=true;
		}
		private function objTop(type:String,target:Object):void{
			var obj:DisplayObject=uiPanel.getChildByName(attrPanel.txt_name.text);
			uiPanel.addChild(obj);
		}
		private function objBottom(type:String,target:Object):void{
			var obj:DisplayObject=uiPanel.getChildByName(attrPanel.txt_name.text);
			uiPanel.addChild(obj);
			for each(var o:DisplayObject in uiPanel){
				if(o!=obj)uiPanel.addChild(o);
			}
		}
		private function hideOther(type:String,target:Object):void{
			var obj:DisplayObject=uiPanel.getChildByName(attrPanel.txt_name.text);
			uiPanel.addChild(obj);
			for each(var o:DisplayObject in uiPanel){
				if(o!=obj)o.visible=false;
			}
		}
		private function objRemove(type:String,target:Object):void{
			var obj:DisplayObject=uiPanel.getChildByName(attrPanel.txt_name.text);
			uiPanel.removeChild(obj);
			attrPanel.visible=false;
		}
		/**键盘操作**/
		private function keyChange(e:KeyboardEvent):void{
			if(!curObj)return;
			if(e.keyCode==37){//左
				curObj.x-=1;
			}else if(e.keyCode==38){//上
				curObj.y-=1;
			}else if(e.keyCode==39){//右
				curObj.x+=1;
			}else if(e.keyCode==40){//下
				curObj.y+=1;
			}else if(e.keyCode==16){//shift
				uiPanel.addChild(curObj);
			}
		}
		/**清空**/
		private function clear(type:String,target:Object):void{
			uiPanel.removeChildren();
			uiPanel.scaleX=1;
			uiPanel.scaleY=1;
			attrPanel.visible=false;
		}
		/**设置显示比例**/
		private function setScale(e:MouseEvent):void{
			if(!e.currentTarget is Stage)return;
			if(e.delta>0){
				uiPanel.scaleX+=0.01;
				uiPanel.scaleY+=0.01;
			}else{
				uiPanel.scaleX-=0.01;
				uiPanel.scaleY-=0.01;
			}
		}
		/**全屏**/
		private function fullScreen(type:String,target:Object):void{
			if(stage.displayState!=StageDisplayState.FULL_SCREEN){
				stage.displayState = StageDisplayState.FULL_SCREEN;
			}else{
				stage.displayState = StageDisplayState.NORMAL;
			}
		}
		/**显示全部**/
		private function showAll(type:String,target:Object):void{
			if(uiPanel&&uiPanel.hasEventListener(MouseEvent.MOUSE_DOWN)){	
				uiPanel.removeEventListener(MouseEvent.MOUSE_DOWN,drag);
				uiPanel.removeEventListener(MouseEvent.MOUSE_UP,sdrag);
				uiPanel["mouseEnabled"]=false;
			}else if(uiPanel){
				uiPanel.addEventListener(MouseEvent.MOUSE_DOWN,drag);
				uiPanel.addEventListener(MouseEvent.MOUSE_UP,sdrag);
				uiPanel["mouseEnabled"]=true;
			}
		}
		/**设置允许拖入资源**/
		private function setDrag(e:NativeDragEvent):void{
			NativeDragManager.acceptDragDrop(uiPanel);
		}
		/**载入拖入的资源**/
		private function load(event:NativeDragEvent):void {
			var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var n:int=files.length;
			uiPanel.doubleClickEnabled=true;
			for(var i:int=0;i<n;i++){
				if(files[i].url.substr(files[i].url.length-3)=="swf"&&menuBtn.txt_class.text.length>0&&menuBtn.txt_class.text!="className"){
					PQLoader.load(files[i].url,{onComplete:loadswf,onCompleteParam:files[i].url});
				}else{
					var mc:Sprite=new Sprite();
					mc.name=unescapeMultiByte(files[i].url);
					var loader:Loader=new Loader();
					loader.load(new URLRequest(files[i].url));
					mc.buttonMode=true;
					mc.mouseChildren=false;
					setResEvent(mc);
					mc.x=mouseX-30;
					mc.y=mouseY-30;
					uiPanel.addChild(mc);
					mc.addChild(loader);
				}
			}
		} 
		/**载入拖入的SWF资源**/
		private function loadswf(...args):void{
			var mc:Sprite=new Sprite();
			mc.name=args[1];
			mc.buttonMode=true;
			mc.mouseChildren=false;
			setResEvent(mc);
			stage.addEventListener(MouseEvent.MOUSE_UP,sdrag);
			var cl:Class=PQLoader.getClassByLibrary(menuBtn.txt_class.text,args[1]) as Class;
			mc.addChild(new cl());
			uiPanel.addChild(mc);
			mc.x=stage.stageWidth/2;
			mc.y=stage.stageHeight/2;
		}
		private function setResEvent(mc:Sprite):void{
			mc.addEventListener(MouseEvent.MOUSE_DOWN,drag);
			mc.addEventListener(MouseEvent.MOUSE_UP,sdrag);
			//mc.addEventListener(MouseEvent.MOUSE_WHEEL,setTop);
			mc.addEventListener(MouseEvent.CLICK,resClick);
		}
		private function resClick(e:MouseEvent):void{
			attrPanel.visible=!attrPanel.visible;
			addChild(attrPanel);
			attrPanel.x=stage.stageWidth/2-attrPanel.width/2;
			attrPanel.y=stage.stageHeight/2-attrPanel.height/2;
			attrPanel.txt_name.text=e.target.name;
		}
		/**对象置顶**/
		private function setTop(type:String,target:Object):void{
			uiPanel.addChild(curObj);
		}
		/**拖动对象**/
		private function drag(e:MouseEvent):void{
			curObj=e.currentTarget as DisplayObject;
			e.currentTarget.startDrag();
			addChild(menuBtn);
		}
		/**停止拖动**/
		private function sdrag(e:MouseEvent):void{
			if(curObj)curObj["stopDrag"]();
			if(mouseX<0||mouseX>stage.stageWidth)curObj.parent.removeChild(curObj);
			else if(mouseY<0||mouseY>stage.stageHeight)curObj.parent.removeChild(curObj);
			addChild(uiPanel);
			addChild(menuBtn);
			addChild(btnDelete);
		}
		/**移除对象**/
		private function delObj(e:MouseEvent):void{
			if(curObj)curObj.parent.removeChild(curObj);
			curObj=null;
		}
		/**获取日期**/
		private function getD():int{
			var d:String="14";
			var da:Date=new Date();
			d+=da.date.toString()+da.hours.toString()+da.minutes;
			return int(d);
		}
		/**输出配置**/
		private function traceConfig(type:String,target:Object):void{
			OutputParsing.traceConfig(uiPanel);
		}
	}
}